import mitt from "mitt";
import { getCurrentInstance, onUnmounted } from "vue";

const emitter = mitt();

// 全局增强函数
emitter.useBusOn = function (eventName, handler) {
  const instance = getCurrentInstance();
  emitter.on(eventName, handler);

  if (instance) {
    onUnmounted(() => emitter.off(eventName, handler));
  }
};

emitter.useBusEmit = function (eventName, ...args) {
  emitter.emit(eventName, ...args);
};

export default emitter;
